
package com.jcp.modelo.dao;

import com.jcp.modelo.Cliente;
import com.jcp.modelo.dao.conexion.Conexion;
import java.io.Serializable;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author DARWIN SILVA VARGAS
 */
public class ClienteDao implements Serializable {

   public static Cliente load(ResultSet rs) throws SQLException{
        Cliente c=new Cliente();
            c.setCodigo(rs.getString("codcliente"));
            c.setCedula(rs.getString("cedula"));
            c.setNombre(rs.getString("nombre"));
            c.setDireccion(rs.getString("direccion"));
            c.setTelefono(rs.getString("telefono"));
            c.setEmail(rs.getString("email"));
        return c;
    }

   private static List cliente;
   private static Conexion conexion;
   private static ClienteDao clientedao;


    private ClienteDao() throws SQLException {
        conexion = Conexion.getInstance();
    }

        public static ClienteDao getInstance() throws SQLException {
        if (clientedao == null) {
            clientedao = new ClienteDao();
        }

        return clientedao;
    }


public static void agregarClientes (Cliente c) throws SQLException, Exception{

    PreparedStatement pst = conexion.getCon().prepareStatement("Insert Into cliente values(?,?,?,?,?,?)");
      pst.setString(1,c.getCodigo());
      pst.setString(2,c.getCedula());
      pst.setString(3,c.getNombre());
      pst.setString(4,c.getDireccion());
      pst.setString(5,c.getTelefono());
      pst.setString(6,c.getEmail());

      pst.executeUpdate();
    }


public static Cliente BuscarCliente(String cod) throws SQLException{
         PreparedStatement pst= conexion.getCon().prepareStatement("select * from cliente where codcliente = ?");
         pst.setString(1, cod);
         ResultSet rs = pst.executeQuery();

         Cliente cli=null;
         while(rs.next()) {
             cli = load(rs);
         }
          return cli;
}

public static boolean ModificarCliente(Cliente c, String cod) throws SQLException, Exception{
    if ( BuscarCliente(cod) == null) {
      throw new SQLException ("Cliente no registrado ");
    }
    boolean res=false;
    PreparedStatement pst = conexion.getCon().prepareStatement("Update cliente set codcliente = ?, cedula = ?, nombre = ?, direccion = ?, telefono = ?, email = ? where codcliente = ?" );
        pst.setString(1,c.getCodigo());
        pst.setString(2,c.getCedula());
        pst.setString(3,c.getNombre());
        pst.setString(4,c.getDireccion());
        pst.setString(5,c.getTelefono());
        pst.setString(6,c.getEmail());
        pst.setString(7,cod);

     int r= pst.executeUpdate();
     
     if (r != 0) {
      res = true;
    }
  
    return res;
}

 public static void EliminarCliente(String cod) throws SQLException, Exception{
     if (BuscarCliente(cod) == null) {
        throw new SQLException("No hay elemento clave de la clase cliente");
     }
     PreparedStatement pst = conexion.getCon().prepareStatement("delete from cliente where codcliente = ?");
        pst.setString(1, cod);
        pst.executeUpdate();

 
}

public static void listarClientes() throws SQLException, Exception {
     ResultSet rs = null;
     cliente = new LinkedList();
     PreparedStatement pst = conexion.getCon().prepareStatement("select * from cliente ");
        rs = pst.executeQuery();

        while(rs.next()) {
           cliente.add(load(rs));
        }
}

public static List getCliente(){
    return cliente;
}

public static void setClientes (List aCliente){
    cliente = aCliente;
}

}
